package com.info.dpunified.dpgen.dataaccess;

import javax.sql.DataSource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.JdbcTemplate;

import com.info.dpunified.web.model.UserProfile;

public class UserManagementDAO {

	protected final Log logger = LogFactory.getLog(getClass());
	
	private JdbcTemplate jdbcTemplate;

	/**
	 * @return the jdbcTemplate
	 */
	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}


	/**
	 * Specify the statement
	 */
	private static final String INSERT_DEFECT_QUERY = "INSERT INTO UserProfile VALUES " +
			"(?,?,?)";

	private static final String LOGIN_CHECK_QUERY = "SELECT count(*) from UserProfile where username = ? and pwd = ? ";
	
	public void setDataSource(DataSource dataSource) {
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}

	public boolean login(UserProfile userProfile)
	{
		boolean loginCheck = true;
		int count = this.getJdbcTemplate().queryForInt(LOGIN_CHECK_QUERY, new Object[]{userProfile.getUsername(), userProfile.getPassword()});
		if(count < 1)
		{
			loginCheck = false;
			logger.info("login failed for username "+userProfile.getUsername() + " and password "+ userProfile.getPassword());
		}
		return loginCheck;
	}
}